package com.myway.controller;

import com.alibaba.fastjson.JSON;
import com.myway.dto.common.JsonData;
import com.myway.dto.common.MD5Util;
import com.myway.pojo.TabMemberInfo;
import com.myway.service.LoginService;
import com.myway.utils.BASE64Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 登陆操作页面
 */
@Controller
@RequestMapping("/en/login")
public class EnLoginController {
    public static final Logger LOG = LoggerFactory.getLogger(EnLoginController.class);
    @Autowired
    private LoginService loginService;


    /**
     * 手机短信验证登陆
     *
     * @param phoneNumber
     * @param code
     * @param request
     * @return
     */
    @RequestMapping("/phoneLogin")
    @ResponseBody
    public JsonData phoneLogin(String phoneNumber, String code, HttpServletRequest request, HttpServletResponse response) {

        JsonData json = loginService.phoneLogin(phoneNumber, code, request, response);
        String status = json.getMsg();
        List<TabMemberInfo> tabMemberInfos = (List<TabMemberInfo>) json.getData();
        if (status.equals("600")) {
            json = new JsonData(true);
            request.getSession().setAttribute("userType", "member");
            request.getSession().setAttribute("member", tabMemberInfos.get(0));
        } else if (status.equals("200")) {
            json = new JsonData(true);
            request.getSession().setAttribute("userType", "user");
            request.getSession().setAttribute("user", phoneNumber);
        } else if (status.equals("404")) {
            json = new JsonData(false);
            json.setMsg("Verification code error, please retrieve.");
        }
        return json;
    }
    /**
     * 会员登陆，先判断会员是否存在，在判断会密码是否正确
     *
     * @param keyword
     * @param membPwd
     * @param request
     * @return
     */
    @RequestMapping("/membLogin")
    @ResponseBody
    public JsonData memberLogin(String keyword, String membPwd, HttpServletRequest request) {
        LOG.info("用户登陆入参" + membPwd + "\t" + keyword);
        System.out.println(membPwd + "    " + keyword);
        JsonData json = loginService.membLogin(keyword);//查找账号
        if (json.isRet()) {//传入的账号格式属于电话，邮箱，或者证件号码。为可用的登陆账号
            List<TabMemberInfo> tabMemberInfo = (List<TabMemberInfo>) json.getData();
            if (MD5Util.string2MD5(BASE64Util.decode(membPwd)).equals(tabMemberInfo.get(0).getMembPwd())) {
                //密码制空，避免存入session中
                tabMemberInfo.get(0).setMembPwd(null);
                request.getSession().setAttribute("userType", "member");
                request.getSession().setAttribute("member", tabMemberInfo.get(0));
                json.setMsg("land successfully");
                return json;
            } else {
                json = new JsonData(false);
                json.setMsg("wrong password");
                return json;
            }
        } else {//传入的账号格式不属于电话，邮箱，或者证件号码。为不可用的登陆账号
            return json;
        }
    }

    /**
     * 用户退出
     *
     * @param session
     * @return
     */
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session) {
        session.removeAttribute("userType");
        session.removeAttribute("member");
        session.removeAttribute("user");
        return "/en/main";
    }


    /**
     * 会员修改密码
     *
     * @param request
     * @param oldPwd
     * @param newPwd
     * @param verificationCode
     * @return
     */
    @RequestMapping("/changePwd")
    @ResponseBody
    public JsonData changePwd(HttpServletRequest request, String oldPwd, String newPwd, String verificationCode) {

        JsonData jsonData = loginService.changePwd(request, oldPwd, newPwd, verificationCode);
        System.out.println(JSON.toJSON(jsonData));
        return jsonData;
    }
}



